package com.wnxy.sk.order.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.sk.common.exception.Assert;
import com.wnxy.sk.order.constant.OrderConstant;
import com.wnxy.sk.order.dto.OrderDto;
import com.wnxy.sk.order.entity.Order;
import com.wnxy.sk.order.entity.OrderItem;
import com.wnxy.sk.order.mapper.OrderItemMapper;
import com.wnxy.sk.order.mapper.OrderMapper;
import com.wnxy.sk.order.service.IOrderItemService;
import com.wnxy.sk.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.sk.order.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2022-10-17
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public String createOrder(OrderDto orderDto) {
        /* 往订单表插入数据 */
        Order order = new Order();
        order.setOrderSn(UUID.randomUUID().toString());
        order.setCreateTime(new Date());
        order.setUserId(orderDto.getUserId());
        //  订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        order.setStatus(OrderConstant.UN_PAY);
        order.setTotalAmount(orderDto.getPromotionPrice());
        // 支付方式：0->未支付；1->支付宝；2->微信
        order.setPayType(OrderConstant.UNPAID);
        // 订单来源：0->PC订单；1->app订单
        order.setSourceType(OrderConstant.SOURCE_TYPE_PC);
        // 订单类型：0->正常订单；1->秒杀订单
        order.setOrderType(OrderConstant.ORDER_TYPE_NORMAL);
        // 保存订单
        this.save(order);

        /* 往订单明细表插入数据 */
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(order.getId());
        orderItem.setOrderSn(order.getOrderSn());
        orderItem.setProductId(orderDto.getProductId());
        orderItem.setProductPic(orderDto.getProductPic());
        orderItem.setProductName(orderDto.getProductName());
        orderItem.setPromotionAmount(orderDto.getPromotionPrice());
        orderItem.setProductPrice(orderDto.getPrice());
        // 保存订单明细
        orderItemMapper.insert(orderItem);
        return order.getOrderSn();
    }

    @Override
    public OrderVo getOrderBySn(String orderSn) {
        Order order = this.getOne(Wrappers.lambdaQuery(Order.class)
                .select(Order::getOrderSn, Order::getTotalAmount, Order::getStatus)
                .eq(Order::getOrderSn, orderSn)
        );
        OrderVo vo = new OrderVo();
        BeanUtils.copyProperties(order,vo);
        return vo;
    }

    @Override
    public Integer updateByOrderSn(String orderSn) {
        //1. 根据订单编号判断该订单是否存在
        LambdaQueryWrapper<Order> qr = Wrappers.lambdaQuery(Order.class);
        qr.eq(Order::getOrderSn,orderSn);
        Long queryCount = baseMapper.selectCount(qr);
        log.info("根据订单编号查询订单 {}",queryCount);
        if(queryCount == 0){
            Assert.error("订单不存在");
        }

        //2. 修改状态为待发货
        LambdaUpdateWrapper<Order> updateWrapper = Wrappers.lambdaUpdate(Order.class);
        updateWrapper.set(Order::getStatus,OrderConstant.PENDING_SHIPMENT);
        updateWrapper.eq(Order::getOrderSn,orderSn);
        this.update(updateWrapper);
        return OrderConstant.PENDING_SHIPMENT;
    }
}
